package com.dingpeikeji.quickdeer.modules.wx.member.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SystemService;
import com.dingpeikeji.quickdeer.modules.base.web.FSmsController;
import com.dingpeikeji.quickdeer.modules.interceptor.FrontBaseInterceptor;
import com.dingpeikeji.quickdeer.modules.utils.ValidateCodeTool;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;
import com.dingpeikeji.quickdeer.modules.wx.member.vo.RegUser;
import com.google.common.collect.Lists;

/**
 * 用户注册
 */
@Controller
@RequestMapping("${wxPath}/member")
public class WxRegisterController extends BaseController{
	
	@Autowired
	private SystemService systemService;

	@RequestMapping(value="register",method=RequestMethod.GET)
	public String register(RegUser regUser, Model model){
		model.addAttribute("regUser", regUser);
		return "wx/modules/member/register";
	}
	
	@RequestMapping(value="register",method=RequestMethod.POST)
	public String doregister(RegUser regUser, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request, String validateCode){
		List<String> messages = Lists.newArrayList(); // 校验的信息集合
		/*List<String> validateCodeMsg = ValidateCodeTool.validateFromPhone(request, validateCode, FSmsController.validateCodeTypes[0]);
		if(!validateCodeMsg.isEmpty()) { // 验证码校验不通过
			messages.addAll(validateCodeMsg);
		} else {
			List<String> valid1 = beanValidatorWithList(regUser);
			if(valid1 != null) {
				messages.addAll(valid1);
			}
			if(!StringUtils.isEmpty(regUser.getPassword()) &&
					!StringUtils.isEmpty(regUser.getRepeatPwd()) && 
					!regUser.getPassword().equals(regUser.getRepeatPwd())) {
				messages.add("两次输入的密码不一致");
			}
		}*/
		if(messages.isEmpty()){
			User query = new User(); // 查找手机号是否绑定
			query.setMobile(regUser.getMobile());
			List<User> userList = systemService.findList(query);
			if(userList != null && !userList.isEmpty()) { // 该手机号已存在
				User exist = userList.get(0); // 集合中的第一个
				if(!ValidateTool.isEmptyOrNull(exist.getWechatId())) { // 已绑定
					messages.add("手机号已绑定");
				} else { // 更新已注册的信息：微信号和修改的密码
					exist.setWechatId(regUser.getWechatId());
					exist.setPassword(SystemService.entryptPassword(regUser.getPassword()));
					systemService.updatePubUser(exist);
					addMessage(redirectAttributes, "绑定成功");
					return "redirect:"+OAuth.OAUTH_BACK_URL;
				}
			} else { // 手机号不存在，新注册用户
				regUser.setName(regUser.getMobile());
				regUser.setLoginName(regUser.getMobile());
				systemService.savePubUser(regUser.toUser());
				addMessage(redirectAttributes, "注册并绑定成功");
				return "redirect:"+OAuth.OAUTH_BACK_URL;
			}
		}
		addMessage(model, messages.toArray(new String[]{}));
		model.addAttribute("regUser", regUser);
		return "wx/modules/member/register";
	}
	
}
